package com.rpay.common.vo;

import com.rpay.common.enums.PcsResultCode;

/**
 * Title：
 * Description
 *
 * @author fxg06 on 2018/4/18
 * @version 1.0
 */
public class PcsResult<V> {
    private int code;
    /**
     * Result message, mainly used for error message.
     */
    private String message;
    /**
     * Result data which contains resource entity.
     */
    protected V data;

    /**
     * Contains expand data of resource entity.
     */
    private Object expand;

    private String desc;

    @Override
    public String toString() {
        return "PcsResult{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", expand=" + expand +
                '}';
    }

    public PcsResult() {
        this(PcsResultCode.SUCCESS);
    }


    public PcsResult(V o) {
        this(PcsResultCode.SUCCESS);
        this.data = o;
    }


    /**
     * 异常码与对应的异常信息
     * @param code EnumPcsService
     */
    public PcsResult(PcsResultCode code) {
        this(code.getCode(), code.getDesc());
    }



    private PcsResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 异常码与自定义的异常信息
     * @param code  EnumPcsService
     * @param message
     */
    public PcsResult(PcsResultCode code, String message) {
        this(code);
        this.message = message;
    }


    public int getCode() {
        return code;
    }


    public PcsResult setCode(int code) {
        this.code = code;
        return this;
    }


    public String getMessage() {
        return message;
    }


    public PcsResult setMessage(String message) {
        this.message = message;
        return this;
    }


    public V getData() {
        return data;
    }


    public PcsResult setData(V data) {
        this.data = data;
        return this;
    }

    public PcsResult setExpandData(Object expand) {
        this.setExpand(expand);
        return this;
    }

    public Object getExpand() {
        return expand;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public void setExpand(Object expand) {
        this.expand = expand;
    }

    public void setErrorCode(PcsResultCode errorCode) {
        this.code = errorCode.getCode();
        this.message = errorCode.getDesc();
    }

    public static PcsResult build(Object data) {
        return PcsResult.build(PcsResultCode.SUCCESS, data);
    }

    public static PcsResult build(PcsResultCode errorCode) {
        return new PcsResult(errorCode);
    }

    public static PcsResult build(PcsResultCode errorCode, Object data) {
        return new PcsResult<Object>(errorCode).setData(data);
    }


    public static PcsResult build(int code, String message) {
        return new PcsResult(code, message);
    }

    public static PcsResult build(int code, String message, Object data) {
        PcsResult<Object> result = new PcsResult(code, message);
        result.setData(data);
        return result;
    }

    /**
     * 返回结果是否正确
     * @return
     */
    public boolean success() {
        return this.getCode() == PcsResultCode.SUCCESS.getCode();
    }
}
